<template>
  <div>
    <el-dialog
      :title="obj.isadd ? '管理员添加' : '管理员编辑'"
      :visible.sync="obj.istrue"
      width="50%"
      :before-close="handleClose"
      @opened="opened"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="一级分类">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择一级分类"
            @change="changefirst"
          >
            <el-option
              v-for="item in catelist"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="form.second_cateid" placeholder="请选择二级分类">
            <el-option
              v-for="item in twolist"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="form.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="市场价格">
          <el-input v-model="form.market_price"></el-input>
        </el-form-item>
        <!-- 图片上传 -->
        <el-form-item label="图片">
          <div class="wrap">
            <h2>+</h2>
            <input
              type="file"
              class="input"
              @change="fileimg"
              v-if="obj.istrue"
            />
            <img :src="imgUrl" alt="" v-if="imgUrl" />
          </div>
        </el-form-item>
        <el-form-item label="商品规格">
          <el-select
            v-model="form.specsid"
            placeholder="请选择商品规格"
            @change="changespecs"
          >
            <el-option
              v-for="item in specslist"
              :key="item.id"
              :label="item.specsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格属性">
          <el-select
            v-model="form.specsattr"
            multiple
            placeholder="请选择规格属性"
          >
            <el-option
              v-for="(item, index) in twoattr"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否新品">
          <el-radio :label="1" v-model="form.isnew">目录</el-radio>
          <el-radio :label="2" v-model="form.isnew">菜单</el-radio>
        </el-form-item>
        <el-form-item label="是否热卖">
          <el-radio :label="1" v-model="form.ishot">目录</el-radio>
          <el-radio :label="2" v-model="form.ishot">菜单</el-radio>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
        <el-form-item label="商品描述">
          <div id="div1" v-if="obj.istrue"></div>
          <!-- <textarea v-model="form.description" cols="30" rows="10"></textarea> -->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isadd"
          >添 加</el-button
        >
        <el-button type="primary" @click="bianji()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { catelist, goodsadd, goodsinfo, goodsedit } from "../../../request/api";
import { successAlert, warningAlert } from "../../../util/alert";
import E from "wangeditor";
export default {
  props: ["obj"],
  data() {
    return {
      imgUrl: "",
      dialogVisible: true,
      twolist: [],
      twoattr: [],
      form: {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 1,
        ishot: 1,
        status: 1,
      },
    };
  },
  methods: {
    ...mapActions({
      reqcatelist: "cate/reqchangelist",
      reqspecslist: "specs/reqchangelist",
      reqgoodslist: "goods/reqchangelist",
      reqgoodscount: "goods/reqchangecount",
    }),
    opened() {
      this.editor = new E("#div1");
      this.editor.create();

      // 给富文本编辑器设置内容
      this.editor.txt.html(this.form.description);
    },
    // 一级分类改变时获取到二级分类列表
    changefirst() {
      catelist({ pid: this.form.first_cateid }).then((res) => {
        console.log(res);
        this.twolist = res.data.list;
        // 清空
        this.form.second_cateid = "";
      });
    },
    // 商品规格改动时的规格属性列表
    changespecs() {
      let data = this.specslist.find((item) => {
        return item.id == this.form.specsid;
      });
      console.log(data);
      this.twoattr = data.attrs;
      this.form.specsattr = "";
    },
    // 点击图片
    fileimg(e) {
      console.log(e);
      // 文件信息
      let file = e.target.files[0];
      if (file.size > 1024 * 1024 * 2) {
        warningAlert("图片大小超过限制");
        return;
      }
      // 文件转换图片地址
      this.imgUrl = URL.createObjectURL(file);
      this.form.img = file;
    },
    // 点击弹窗x执行的回调函数
    handleClose() {
      this.quxiao();
    },
    // 清空数据
    empty() {
      this.form = {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 1,
        ishot: 1,
        status: 1,
      };
    },
    // 点击取消
    quxiao() {
      // 取消弹窗
      this.obj.istrue = false;
      // 清空图片
      this.imgUrl = "";
      //   清空内容
      this.empty();
    },
    // 点击添加
    add() {
      this.form.specsattr = JSON.stringify(this.form.specsattr);
      this.form.description = this.editor.txt.html()
      console.log(this.form);
      goodsadd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          // 关闭弹框清空内容
          this.quxiao();
          // 刷新列表
          this.reqgoodslist();
          // 刷新总个数
          this.reqgoodscount();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条信息
    getinfo(id) {
      goodsinfo({ id: id }).then((res) => {
        this.form = res.data.list;
        this.imgUrl = this.$pre + this.form.img;
        this.form.specsattr = JSON.parse(this.form.specsattr);
        // 重新渲染二级分类
        catelist({ pid: this.form.first_cateid }).then((res) => {
          this.twolist = res.data.list;
        });
        // 重新渲染规格属性
        this.twoattr = this.specslist.find(
          (item) => item.id == this.form.specsid
        ).attrs;
        this.form.id = id;
      });
    },
    // 点击编辑
    bianji() {
      this.form.specsattr = JSON.stringify(this.form.specsattr);
      this.form.description = this.editor.txt.html()
      console.log(this.form);
      console.log(goodsedit);
      goodsedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          // 关闭弹框清空内容
          this.quxiao();
          // 刷新列表
          this.reqgoodslist();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  computed: {
    ...mapGetters({
      catelist: "cate/list",
      specslist: "specs/list",
    }),
  },
  mounted() {
    this.reqspecslist(true);
    this.reqcatelist();
  },
};
</script>

<style lang="less" scoped>
.wrap {
  width: 200px;
  height: 200px;
  border: 1px dashed #ccc;
  text-align: center;
  position: relative;
}
.wrap h2 {
  font-size: 50px;
  line-height: 200px;
  margin: 0 auto;
}
.wrap img {
  position: absolute;
  top: 0;
  left: 0;
  width: 200px;
  height: 200px;
  z-index: 2;
}
.wrap .input {
  position: absolute;
  top: 0;
  left: 0;
  width: 200px;
  height: 200px;
  border: none;
  outline: none;
  opacity: 0;
  z-index: 3;
}
</style>